package com.wjj.getip.test;


import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

public class Signature {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
    private static final String CHARSET = "utf-8";

    public static byte[] HmacSHA1(String text, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        byte[] data = key.getBytes(CHARSET);
        SecretKeySpec secretKeySpec = new SecretKeySpec(data, HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(secretKeySpec);
        byte[] textBytes = text.getBytes(CHARSET);
        return mac.doFinal(textBytes);
    }

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long l =  1465185768;
        long l2 = 1595828525;
        String format = simpleDateFormat.format(l2);
        System.out.println(format);*/

        /*String word = "0EEm/HtGRr/VJXTAD9tYMth1Bzm3l****LHz5RCDv1GdM8s=";
        String encode = URLEncoder.encode(word, "UTF-8");
        System.out.println(encode);*/

/*        String key = "Gu5t9xGAR****************CN3Cozk1qA";
        String text = "GETcvm.api.qcloud.com/v2/index.php?Action=DescribeInstances&InstanceIds.0=ins-09dx96dg&Nonce=11886&Region=ap-guangzhou&SecretId=AKIDz8krbsJ****************Px3gnPhESA&SignatureMethod=HmacSHA256&Timestamp=1465185768";
        byte[] bytes = HmacSHA1(text, key);
        String s = Base64.getEncoder().encodeToString(bytes);
        System.out.println(s);*/
/*        Date date = new Date();
        System.out.println(date.getTime());*/
String s = "\\u8bf7\\u6c42\\u5931\\u8d25\\uff0c\\u53c2\\u6570[domain]\\u4e0d\\u80fd\\u4e3a\\u7a7a\\u3002";
        String decode = URLDecoder.decode(s, "utf-8");
        System.out.println(decode);
    }
}
